package org.proyectosinapsis.Sinapsis;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import org.proyectosinapsis.Sinapsis.library.InfoArrayAdapter;
import org.proyectosinapsis.Sinapsis.library.JSONParser;

import java.util.ArrayList;
import java.util.List;

/**
 * User: Alexander Simoes
 * Company: CoralVision Systems
 * Date: 9/25/12
 */
public class PatientShowActivity extends ListActivity {   //ToDo: logo back listener

	private ProgressDialog pDialog;
	private InfoArrayAdapter arrayAdapter;
	private String[][] values;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.patient_data);

		//Get option and pid from previous activity
		Intent intent = getIntent();
		int option = intent.getIntExtra(PatientHomeActivity.USER_OPTION,0);
		String pid = intent.getStringExtra(PatientHomeActivity.USER_PID);

		TextView textView = (TextView) findViewById(R.id.message);
		//Initialize global variables
		switch (option) {
			case PatientHomeActivity.OPT_PERS_DATA:
				values = new String[33][2];
				break;
			case PatientHomeActivity.OPT_IMMU:
				values = new String[17][2];
				textView.setText(R.string.lastImmu);
				break;
			case PatientHomeActivity.OPT_BACK_FAM:
				values = new String[22][2];
				textView.setText(R.string.backFam);
				break;
			case PatientHomeActivity.OPT_BACK_PER:
				values = new String[69][2];
				textView.setText(R.string.backPer);
				break;
			case PatientHomeActivity.OPT_BACK_PSI:
				break;
			case PatientHomeActivity.OPT_PHYS:
				break;
			case PatientHomeActivity.OPT_LAB:
				break;
			case PatientHomeActivity.OPT_SOAP:
				break;
			default: values = new String[1][2];
				values[0][0] = "INTERNAL ERROR";
				values[0][1] = "INVALID OPTION IN INTENT";
		}

		arrayAdapter = new InfoArrayAdapter(PatientShowActivity.this, R.layout.row, values);

		//Bound list with adapter
		setListAdapter(arrayAdapter);

		//Make the http call in another thread
		new httpCall().execute(""+option,pid);
	}

	private class httpCall extends AsyncTask<String, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(PatientShowActivity.this);
			pDialog.setMessage(getString(R.string.wait));
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		@Override
		protected Void doInBackground(String... params) {
			JSONParser jParser = new JSONParser();
			String url = getString(R.string.URL_SINAPSIS_ANDROID);
			String tagSuccess = getString(R.string.TAG_SUCCESS);

			int option = Integer.parseInt(params[0]);
			String pid = params[1];

			//Building Parameters
			List<NameValuePair> jsonParams = new ArrayList<NameValuePair>();
			jsonParams.add(new BasicNameValuePair("pid", pid));

			if (option == PatientHomeActivity.OPT_PERS_DATA) { //Patient Data
				jsonParams.add(new BasicNameValuePair("tag", "patientData"));
				JSONObject json = jParser.getJSONFromUrl(url, jsonParams);

				try {
					int success = json.getInt(tagSuccess);
					if (success == 1) {
						JSONObject user = json.getJSONObject("user");
						fillArrayWithJson(option,user);
					}
//					 else {   //ToDo:Check error from php API
//						error = true;
//					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else if (option == PatientHomeActivity.OPT_BACK_FAM) { //Patient Familiar Background
				jsonParams.add(new BasicNameValuePair("tag", "patBackFam"));
				JSONObject json = jParser.getJSONFromUrl(url, jsonParams);

				try {
					int success = json.getInt(tagSuccess);
					if (success == 1) {
						JSONObject user = json.getJSONObject("user");
						fillArrayWithJson(option,user);
					}
//					 else {   //ToDo:Check error from php API
//						error = true;
//					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			 else if (option == PatientHomeActivity.OPT_BACK_PER) { //Patient Personal Background
				jsonParams.add(new BasicNameValuePair("tag", "patBackPer"));
				JSONObject json = jParser.getJSONFromUrl(url, jsonParams);

				try {
					int success = json.getInt(tagSuccess);
					if (success == 1) {
						JSONObject user = json.getJSONObject("user");
						fillArrayWithJson(option,user);
					}
		//					 else {   //ToDo:Check error from php API
		//						error = true;
		//					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
		} else if (option == PatientHomeActivity.OPT_IMMU) { //Patient Immunizations
				jsonParams.add(new BasicNameValuePair("tag", "patientImmu"));
				JSONObject json = jParser.getJSONFromUrl(url, jsonParams);

				try {
					int success = json.getInt(tagSuccess);
					if (success == 1) {
						JSONObject user = json.getJSONObject("user");
						fillArrayWithJson(option,user);
					}
//					 else {   //ToDo:Check error from php API (no record exists)
//						error = true;
//					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void vd) {
			pDialog.dismiss();
			arrayAdapter.notifyDataSetChanged();
		}

		private void fillArrayWithJson(int option, JSONObject user) {
			try {
				/*
									  values[][0] = getString(R.string);
									  values[][1] = user.getString("");
									 */

				if (option == PatientHomeActivity.OPT_PERS_DATA) {
					values[0][0] = getString(R.string.pShow01_PID);
					values[0][1] = user.getString("pid");
					values[1][0] = getString(R.string.pShow02_Reg);
					values[1][1] = user.getString("date_reg");
					values[2][0] = getString(R.string.pShow03_LastName);
					values[2][1] = user.getString("name_last");
					values[3][0] = getString(R.string.pShow04_FirstName);
					values[3][1] = user.getString("name_first");
					values[4][0] = getString(R.string.pShow05_ID);
					values[4][1] = user.getString("nat_id_nr");
					values[5][0] = getString(R.string.pShow06_Eth);
					values[5][1] = user.getString("ethnic_orig");
					values[6][0] = getString(R.string.pShow07_Birth);
					values[6][1] = user.getString("date_birth");
					values[7][0] = getString(R.string.pShow08_Sex);
					values[7][1] = user.getString("sex");
					values[8][0] = getString(R.string.pShow09_Blood);
					values[8][1] = user.getString("blood_group");
					values[9][0] = getString(R.string.pShow10_BthCountry);
					values[9][1] = user.getString("birth_country");
					values[10][0] = getString(R.string.pShow11_BthCity);
					values[10][1] = user.getString("birth_city");
					values[11][0] = getString(R.string.pShow12_BthEntity);
					values[11][1] = user.getString("birth_entity");
					values[12][0] = getString(R.string.pShow13_BthTown);
					values[12][1] = user.getString("birth_town");
					values[13][0] = getString(R.string.pShow14_CivilStatus);
					values[13][1] = user.getString("civil_status");
					values[14][0] = getString(R.string.pShow15_Illiterate);
					values[14][1] = user.getString("illiterate");
					values[15][0] = getString(R.string.pShow16_EducLevel);
					values[15][1] = user.getString("educational_level");
					values[16][0] = getString(R.string.pShow17_EducPlan);
					values[16][1] = user.getString("educational_plan");
					values[17][0] = getString(R.string.pShow18_YearsAppr);
					values[17][1] = user.getString("years_approved");
					values[18][0] = getString(R.string.pShow19_Prof);
					values[18][1] = user.getString("profession");
					values[19][0] = getString(R.string.pShow20_SSO);
					values[19][1] = user.getString("sso");
					values[20][0] = getString(R.string.pShow21_AddrEnt);
					values[20][1] = user.getString("addr_entity");
					values[21][0] = getString(R.string.pShow22_AddrCityTown);
					values[21][1] = user.getString("addr_citytown_name");
					values[22][0] = getString(R.string.pShow23_AddrTown);
					values[22][1] = user.getString("addr_town");
					values[23][0] = getString(R.string.pShow24_AddrLocality);
					values[23][1] = user.getString("addr_locality");
					values[24][0] = getString(R.string.pShow25_AddrSector);
					values[24][1] = user.getString("addr_sector");
					values[25][0] = getString(R.string.pShow26_AddrStr);
					values[25][1] = user.getString("addr_str");
					values[26][0] = getString(R.string.pShow27_AddrHouse);
					values[26][1] = user.getString("addr_house");
					values[27][0] = getString(R.string.pShow28_AddrFloor);
					values[27][1] = user.getString("addr_floor");
					values[28][0] = getString(R.string.pShow29_AddrRef);
					values[28][1] = user.getString("addr_reference");
					values[29][0] = getString(R.string.pShow30_AddrTime);
					values[29][1] = user.getString("addr_time");
					values[30][0] = getString(R.string.pShow31_MotherName);
					values[30][1] = user.getString("name_mother");
					values[31][0] = getString(R.string.pShow32_FatherName);
					values[31][1] = user.getString("name_father");
					values[32][0] = getString(R.string.pShow33_RegisterBy);
					values[32][1] = user.getString("create_id");

				} else if (option == PatientHomeActivity.OPT_BACK_FAM) {
					values[0][0] = getString(R.string.pBackF00);
					values[0][1] = "("+user.getString("alcoholismo")+") "+user.getString("alcoholismo_txt");
					values[1][0] = getString(R.string.pBackF01);
					values[1][1] = "("+user.getString("alergia")+") "+user.getString("alergia_txt");
					values[2][0] = getString(R.string.pBackF02);
					values[2][1] = "("+user.getString("artritis")+") "+user.getString("artritis_txt");
					values[3][0] = getString(R.string.pBackF03);
					values[3][1] = "("+user.getString("asma")+") "+user.getString("asma_txt");
					values[4][0] = getString(R.string.pBackF04);
					values[4][1] = "("+user.getString("cancer")+") "+user.getString("cancer_txt");
					values[5][0] = getString(R.string.pBackF05);
					values[5][1] = "("+user.getString("cardiopatia")+") "+user.getString("cardiopatia_txt");
					values[6][0] = getString(R.string.pBackF06);
					values[6][1] = "("+user.getString("desnutricion")+") "+user.getString("desnutricion_txt");
					values[7][0] = getString(R.string.pBackF07);
					values[7][1] = "("+user.getString("diabetes")+") "+user.getString("diabetes_txt");
					values[8][0] = getString(R.string.pBackF08);
					values[8][1] = "("+user.getString("drogas")+") "+user.getString("drogas_txt");
					values[9][0] = getString(R.string.pBackF09);
					values[9][1] = "("+user.getString("enf_renal")+") "+user.getString("enf_renal_txt");
					values[10][0] = getString(R.string.pBackF10);
					values[10][1] = "("+user.getString("gastropatia")+") "+user.getString("gastropatia_txt");
					values[11][0] = getString(R.string.pBackF11);
					values[11][1] = "("+user.getString("HTA_sist")+") "+user.getString("HTA_sist_txt");
					values[12][0] = getString(R.string.pBackF12);
					values[12][1] = "("+user.getString("malf_congenita")+") "+user.getString("malf_congenita_txt");
					values[13][0] = getString(R.string.pBackF13);
					values[13][1] = "("+user.getString("neurologica")+") "+user.getString("neurologica_txt");
					values[14][0] = getString(R.string.pBackF14);
					values[14][1] = "("+user.getString("obesidad")+") "+user.getString("obesidad_txt");
					values[15][0] = getString(R.string.pBackF15);
					values[15][1] = "("+user.getString("sifilis_its")+") "+user.getString("sifilis_its_txt");
					values[16][0] = getString(R.string.pBackF16);
					values[16][1] = "("+user.getString("tabaquismo")+") "+user.getString("tabaquismo_txt");
					values[17][0] = getString(R.string.pBackF17);
					values[17][1] = "("+user.getString("tuberculosis")+") "+user.getString("tuberculosis_txt");
					values[18][0] = getString(R.string.pBackF18);
					values[18][1] = "("+user.getString("t_mentales")+") "+user.getString("t_mentales_txt");
					values[19][0] = getString(R.string.pBackF19);
					values[19][1] = "("+user.getString("varices")+") "+user.getString("varices_txt");
					values[20][0] = getString(R.string.pBackF20);
					values[20][1] = "("+user.getString("vih_sida")+") "+user.getString("vih_sida_txt");
					values[21][0] = getString(R.string.pBackF21);
					values[21][1] = "("+user.getString("otras")+") "+user.getString("otras_txt");

				} else if (option == PatientHomeActivity.OPT_BACK_PER) {
					values[0][0] = getString(R.string.pBackP00);
					values[0][1] = "";//user.getString("");
					values[1][0] = getString(R.string.pBackP01);
					values[1][1] = user.getString("pubarquia");
					values[2][0] = getString(R.string.pBackP02);
					values[2][1] = "";//user.getString("");
					values[3][0] = getString(R.string.pBackP03);
					values[3][1] = user.getString("inicio_crecimiento_tetiscular");
					values[4][0] = getString(R.string.pBackP04);
					values[4][1] = "";//user.getString("");
					values[5][0] = getString(R.string.pBackP05);
					values[5][1] = user.getString("primera_relacion");
					values[6][0] = getString(R.string.pBackP06);
					values[6][1] = user.getString("frecuencia_relacion");
					values[7][0] = getString(R.string.pBackP07);
					values[7][1] = user.getString("numero_parejas");
					values[8][0] = getString(R.string.pBackP08);
					values[8][1] = user.getString("relacion_sexual_satisfactoria");
					values[9][0] = getString(R.string.pBackP09);
					values[9][1] = user.getString("anticoncepcion");
					values[10][0] = getString(R.string.pBackP10);
					values[10][1] = user.getString("metodo_anticoncepcion");
					values[11][0] = getString(R.string.pBackP11);
					values[11][1] = "";//user.getString("");
					values[12][0] = getString(R.string.pBackP12);
					values[12][1] = "";//user.getString("");
					values[13][0] = getString(R.string.pBackP13);
					values[13][1] = "";//user.getString("");
					values[14][0] = getString(R.string.pBackP14);
					values[14][1] = "";//user.getString("");
					values[15][0] = getString(R.string.pBackP15);
					values[15][1] = "";//user.getString("");
					values[16][0] = getString(R.string.pBackP16);
					values[16][1] = user.getString("menopausia_andropausia");
					values[17][0] = getString(R.string.pBackP17);
					values[17][1] = user.getString("fuma");
					values[18][0] = getString(R.string.pBackP18);
					values[18][1] = user.getString("fuma_desde");
					values[19][0] = getString(R.string.pBackP19);
					values[19][1] = user.getString("numero_cigarros_por_dia");
					values[20][0] = getString(R.string.pBackP20);
					values[20][1] = user.getString("alcohol");
					values[21][0] = getString(R.string.pBackP21);
					values[21][1] = user.getString("cantidad_alcohol_semanal");
					values[22][0] = getString(R.string.pBackP22);
					values[22][1] = user.getString("drogas_ilicitas");
					values[23][0] = getString(R.string.pBackP23);
					values[23][1] = user.getString("actividad_fisica");
					values[24][0] = getString(R.string.pBackP24);
					values[24][1] = user.getString("sedentarismo");
					values[25][0] = getString(R.string.pBackP25);
					values[25][1] = user.getString("manejo_estres");
					values[26][0] = getString(R.string.pBackP26);
					values[26][1] = user.getString("tumor_benigno");
					values[27][0] = getString(R.string.pBackP27);
					values[27][1] = user.getString("tumor_maligno");
					values[28][0] = getString(R.string.pBackP28);
					values[28][1] = user.getString("enf_eruptivas");
					values[29][0] = getString(R.string.pBackP29);
					values[29][1] = user.getString("its");
					values[30][0] = getString(R.string.pBackP30);
					values[30][1] = user.getString("meingitis");
					values[31][0] = getString(R.string.pBackP31);
					values[31][1] = user.getString("chagas");
					values[32][0] = getString(R.string.pBackP32);
					values[32][1] = user.getString("tuberculosis_personales");
					values[33][0] = getString(R.string.pBackP33);
					values[33][1] = user.getString("dengue");
					values[34][0] = getString(R.string.pBackP34);
					values[34][1] = user.getString("hansen");
					values[35][0] = getString(R.string.pBackP35);
					values[35][1] = user.getString("leishmaniasis");
					values[36][0] = getString(R.string.pBackP36);
					values[36][1] = user.getString("leptopirosis");
					values[37][0] = getString(R.string.pBackP37);
					values[37][1] = user.getString("malaria");
					values[38][0] = getString(R.string.pBackP38);
					values[38][1] = user.getString("desnutricion_patologia");
					values[39][0] = getString(R.string.pBackP39);
					values[39][1] = user.getString("diabetes_patologia");
					values[40][0] = getString(R.string.pBackP40);
					values[40][1] = user.getString("dislipidemias");
					values[41][0] = getString(R.string.pBackP41);
					values[41][1] = user.getString("obesidad_patologia");
					values[42][0] = getString(R.string.pBackP42);
					values[42][1] = user.getString("trastorno_apetito");
					values[43][0] = getString(R.string.pBackP43);
					values[43][1] = user.getString("enuresis");
					values[44][0] = getString(R.string.pBackP44);
					values[44][1] = user.getString("chupa_dedo");
					values[45][0] = getString(R.string.pBackP45);
					values[45][1] = user.getString("onicofagia");
					values[46][0] = getString(R.string.pBackP46);
					values[46][1] = user.getString("trastorno_llanto");
					values[47][0] = getString(R.string.pBackP47);
					values[47][1] = user.getString("hta_sistemica");
					values[48][0] = getString(R.string.pBackP48);
					values[48][1] = user.getString("tromboembolismo");
					values[49][0] = getString(R.string.pBackP49);
					values[49][1] = user.getString("varices_patologia");
					values[50][0] = getString(R.string.pBackP50);
					values[50][1] = user.getString("cardiopatia_patologia");
					values[51][0] = getString(R.string.pBackP51);
					values[51][1] = user.getString("asma_patologia");
					values[52][0] = getString(R.string.pBackP52);
					values[52][1] = user.getString("neumonia");
					values[53][0] = getString(R.string.pBackP53);
					values[53][1] = user.getString("gastroenteropatias");
					values[54][0] = getString(R.string.pBackP54);
					values[54][1] = user.getString("hepatopatias");
					values[55][0] = getString(R.string.pBackP55);
					values[55][1] = user.getString("tras_evacuacion");
					values[56][0] = getString(R.string.pBackP56);
					values[56][1] = user.getString("colagenopatias");
					values[57][0] = getString(R.string.pBackP57);
					values[57][1] = user.getString("artritis_patologia");
					values[58][0] = getString(R.string.pBackP58);
					values[58][1] = user.getString("tras_micciones");
					values[59][0] = getString(R.string.pBackP59);
					values[59][1] = user.getString("enfermedad_renal");
					values[60][0] = getString(R.string.pBackP60);
					values[60][1] = user.getString("alergias");
					values[61][0] = getString(R.string.pBackP61);
					values[61][1] = user.getString("trastornos_sueno");
					values[62][0] = getString(R.string.pBackP62);
					values[62][1] = user.getString("violencia_psicologica");
					values[63][0] = getString(R.string.pBackP63);
					values[63][1] = user.getString("violencia_fisica");
					values[64][0] = getString(R.string.pBackP64);
					values[64][1] = user.getString("violencia_sexual");
					values[65][0] = getString(R.string.pBackP65);
					values[65][1] = user.getString("accidentes");
					values[66][0] = getString(R.string.pBackP66);
					values[66][1] = user.getString("hospitalizacion");
					values[67][0] = getString(R.string.pBackP67);
					values[67][1] = user.getString("intervencion_quirurgica");
					values[68][0] = getString(R.string.pBackP68);
					values[68][1] = user.getString("otros2");

				} else if (option == PatientHomeActivity.OPT_IMMU) {
					values[0][0] = getString(R.string.pIMMU00_bcg);
					values[0][1] = user.getString("bcg");
					values[1][0] = getString(R.string.pIMMU01_antipol);
					values[1][1] = user.getString("antipoliomelitica");
					values[2][0] = getString(R.string.pIMMU02_pentav);
					values[2][1] = user.getString("pentavalente");
					values[3][0] = getString(R.string.pIMMU03_antirota);
					values[3][1] = user.getString("antirotavirus");
					values[4][0] = getString(R.string.pIMMU04_antineumo);
					values[4][1] = user.getString("antineumococcica");
					values[5][0] = getString(R.string.pIMMU05_antihepa);
					values[5][1] = user.getString("antihepatitisB");
					values[6][0] = getString(R.string.pIMMU06_triple);
					values[6][1] = user.getString("triple_bacteriana");
					values[7][0] = getString(R.string.pIMMU07_trival);
					values[7][1] = user.getString("trivalente_viral");
					values[8][0] = getString(R.string.pIMMU08_antiamar);
					values[8][1] = user.getString("antiamarilica");
					values[9][0] = getString(R.string.pIMMU09_dobleviral);
					values[9][1] = user.getString("doble_viral");
					values[10][0] = getString(R.string.pIMMU10_antihaem);
					values[10][1] = user.getString("antihaemof_influenza_B");
					values[11][0] = getString(R.string.pIMMU11_antimenin);
					values[11][1] = user.getString("antineumococcica_BC"); //The name differs, but it was the order
					values[12][0] = getString(R.string.pIMMU12_tox_tet);
					values[12][1] = user.getString("toxoide_tetanico");
					values[13][0] = getString(R.string.pIMMU13_tox_dif);
					values[13][1] = user.getString("toxoide_difterico");
					values[14][0] = getString(R.string.pIMMU14_anti_inf);
					values[14][1] = user.getString("antiinfluenza");
					values[15][0] = getString(R.string.pIMMU15_others);
					values[15][1] = user.getString("otras");
					values[16][0] = getString(R.string.pIMMU16_others_det);
					values[16][1] = user.getString("otras_especificacion");
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
}